<?php
/**
 * Created by PhpStorm.
 * User: lullabies
 * Date: 2019/5/22
 * Time: 9:45
 */

namespace app\api\model;


use think\Model;
//考试模型
class Exam extends  Model
{
    //设置当前数据表对应的完整数据表名称
//    protected $table = 'art_exam';
    //设置隐藏字段
    protected $hidden = ['delete_time','status','update_time','tab'];
    /**
     * 设置与总分数的一对多关联
     */
    public function totalGrade(){
        return $this->hasMany('TotalGrade','exam_id','id');
    }
    /**
     * 设置与科目一对多的关联关系
     */
    public function subjects(){
        return $this->hasMany('subjects','exam_id','id');
    }

    /**
     * 根据地区名称获取模拟考试的相关信息
     */
    public  static function getInfoByRegion($region){
        $result = self::where('region','=',$region)
            ->with(['subjects'=>function($query){
            $query->field('exam_id,name')->where('status',1);
        }])->withCount(['totalGrade'=>'total_user'])->find();
        return $result;
    }
    /**
     * 根据模拟考试的id获取学生分数 排名等信息
     */
    public static function getGradeById($id){
        $result = self::field('id,title,region,school')->with(['totalGrade'=>function($query){
            $query->field('id,exam_id,user_id,score')->order('score','desc')
                ->with(['user'=>function($query){
                    $query->field('id,username,img,school,gender')->where('status',1);
                }]);
        }])->find($id);
        return $result;
    }
    /**
     * 获取模拟考试的考试说明及科目
     */
    public static function getExamInfoById($id){
        $result = self::with(['subjects' => function($query){
            $query->hidden(['status','update_time','create_time'])->where('status',1);
        }])->find($id);
        return $result;
    }
}